package com.cheys.webapp.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cheys.webapp.dao.CaseDao;
import com.cheys.webapp.model.BBS;
import com.cheys.webapp.model.Case;
import com.cheys.webapp.model.Page;
import com.cheys.webapp.model.User;
import com.cheys.webapp.util.DateUtil;
import com.cheys.webapp.util.LogUtil;
import com.cheys.webapp.util.StringUtil;
@Service
public class CaseService implements IService<Case> {
	@Autowired
	private CaseDao dao;
	
	private CopyOnWriteArrayList<Case> list = new CopyOnWriteArrayList<Case>();
	
	public int save(Case[] t) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		sql.append("insert into ");
		sql.append(Case.TABLE_NAME);
		sql.append("(title,content,resource,fm_resource,type,typeName,belong,updatetime,isneturl,parent_id) values ");
		for (Case cc : t) {
			sql.append("(");
			sql.append("'"+cc.getTitle()+"'");
			sql.append(",");
			sql.append("'"+cc.getContent()+"'");
			sql.append(",");
			sql.append("'"+cc.getResource()+"'");
			sql.append(",");
			sql.append("'"+cc.getFmResource()+"'");
			sql.append(",");
			sql.append(cc.getType());
			sql.append(",");
			sql.append("'"+cc.getTypeName()+"'");
			sql.append(",");
			sql.append(cc.getBelong());
			sql.append(",");
			sql.append(DateUtil.now(DateUtil.DATE_FORMAT_YYYYMMDDHHMMSS));
			sql.append(",");
			sql.append(cc.getIsneturl());
			sql.append(",");
			sql.append(cc.getParentId());
			sql.append("),");
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(";");
		int save = this.dao.save(sql.toString());
		if(save > 0){
			initList();
		}
		return save;
	}

	public int update(Case c) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sbWhere = new StringBuffer();
		Case info = this.dao.selectOne(c.getId());
		if(StringUtil.isNotBlank(c.getTitle()) && ! c.getTitle().equals(info.getTitle())){
			sbWhere.append("title = '"+c.getTitle()+"'");
			sbWhere.append(",");
		}
		if(StringUtil.isNotBlank(c.getContent()) && ! c.getContent().equals(info.getContent())){
			sbWhere.append("content = '"+c.getContent()+"'");
			sbWhere.append(",");
		}
		if(StringUtil.isNotBlank(c.getResource()) && ! c.getResource().equals(info.getResource())){
			sbWhere.append("resource = '"+c.getResource()+"'");
			sbWhere.append(",");
		}
		if(StringUtil.isNotBlank(c.getFmResource()) && ! c.getFmResource().equals(info.getFmResource())){
			sbWhere.append("fm_resource = '"+c.getFmResource()+"'");
			sbWhere.append(",");
		}
		if(StringUtil.isNotBlank(c.getIsneturl()) && ! c.getIsneturl().equals(info.getIsneturl())){
			sbWhere.append("isneturl = '"+c.getIsneturl()+"'");
			sbWhere.append(",");
		}
		if(c.getParentId() !=0 && c.getParentId() != info.getParentId()){
			sbWhere.append("parent_id = "+c.getParentId());
			sbWhere.append(",");
		}
		if(sbWhere.length() == 0){
			return 0;
		}else{
			sbWhere.append("updatetime = '"+DateUtil.now(DateUtil.DATE_FORMAT_YYYYMMDDHHMMSS)+"'");
			sbWhere.append(",");
		}
		if(sbWhere.toString().endsWith(",")){
			sbWhere.deleteCharAt(sbWhere.lastIndexOf(","));
		}
		sbWhere.append(" where id="+c.getId());
		String sql = "update "+Case.TABLE_NAME+" set "+ sbWhere.toString();
		LogUtil.getLogger(getClass()).info("sql:"+sql);
		int update = this.dao.update(sql);
		if(update > 0){
			initList();
		}
		return update;
	}

	public int delete(int[] id) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		
		for (int i : id) {
			sb.append(i);
			sb.append(",");
		}
		sb.deleteCharAt(sb.lastIndexOf(","));
		String sql = "delete from "+Case.TABLE_NAME+" where id in("+sb.toString()+")";
		int delete = this.dao.delete(sql);
		if(delete > 0){
			List<Case> deletes = new ArrayList<Case>();
			for (int i : id) {
				for (Case case1 : list) {
					if(case1.getId() == i){
						deletes.add(case1);
						break;
					}
				}
			}
			list.removeAll(deletes);
		}
		return delete;
	}

	public Case selectOne(int id) throws Exception {
		// TODO Auto-generated method stub
		Case c = null;
		for (Case case1 : getList()) {
			if(case1.getId() == id){
				c = case1;
				break;
			}
		}
		if(c == null){
			c = this.dao.selectOne(id);
			if(c != null){
				initList();
			}
		}
		return c;
	}

	public List<Case> select(Case t,Page page) throws Exception {
		// TODO Auto-generated method stub
		CopyOnWriteArrayList<Case> list1 = getList();
		if(list1 == null || list1.size() ==0){
			return this.dao.select(t);
		}
		if(t != null && list1 != null && list1.size() > 0){
			for (Case case1 : list1) {
				if(t.getId() !=0 && t.getId() != case1.getId()){
					list1.remove(case1);
				}
				if(t.getTitle() != null && !case1.getTitle().contains(t.getTitle())){
					list1.remove(case1);
				}
				if(t.getContent() != null && !case1.getContent().contains(t.getContent())){
					list1.remove(case1);
				}
				if(t.getType() != 0 && case1.getType() != t.getType()){
					list1.remove(case1);
				}
				if(t.getBelong() != 0 && case1.getBelong() != t.getBelong()){
					list1.remove(case1);
				}
				if(t.getParentId() !=0 && case1.getParentId() != t.getParentId()){
					list1.remove(case1);
				}
			}
		}else{
			return null;
		}
		List<Case> select = new ArrayList<Case>();
		for (Case case1 : list1) {
			select.add(case1);
		}
		if(page != null && select.size() > 0){
			page.setTotalRecord(select.size());
			select = select.subList((page.getCurrentpage()-1) * page.getPageSize(), page.getCurrentpage() * page.getPageSize());
		}
		return select;
	}
	public List<Case> getCase(Case c) throws Exception {
		// TODO Auto-generated method stub
		List<Case> select = select(c, null);
		if(select == null || select.size() == 0)
			select  = this.dao.select(c);
		return select;
	}
	
	public int addCase(Case c) throws Exception {
		// TODO Auto-generated method stub
		return this.save(new Case[]{c});
	}
	public int delete(Case c) throws Exception {
		// TODO Auto-generated method stub
		return this.delete(new int[]{c.getId()});
	}
	public List<BBS> getBBSInfoByCaseId(int caseId){
		
		return this.dao.getBBSInfoByCaseId(caseId);
	}
	public int deleteBBSInfoByCaseId(int caseId) throws Exception{
		String sql = "delete from "+BBS.TABLE_NAME+" b where b.case_id = "+caseId;
		return this.dao.delete(sql);
	}
	public void initList(){
		try {
			List<Case> select = this.dao.select(null);
			CopyOnWriteArrayList<Case> cases = new CopyOnWriteArrayList<Case>();
			for (Case c : select) {
				cases.add(c);
			}
			setList(cases);
		} catch (Exception e) {
			LogUtil.getLogger().debug("initList have an Exception ,Message :"+e);
		}
	}

	public CopyOnWriteArrayList<Case> getList() throws Exception {
		if(list.size() == 0){
			list.addAll(this.dao.select(null));
		}
		return list;
	}

	public void setList(CopyOnWriteArrayList<Case> list) {
		this.list = list;
	}

	public int saveBBS(BBS bbs) throws Exception {
		// TODO Auto-generated method stub
		int user_id = 0;
		User user = bbs.getUser();
		if(null == user){
			user_id = 99999999;
		}
		String sql = "insert into "+ BBS.TABLE_NAME +"(case_id,content,user_id) values("+bbs.getCaseId()+",'"+bbs.getContent()+"',"+user_id+")";
		int save = this.dao.save(sql);
		return save;
	}

	public int delBBSById(int id) throws Exception {
		// TODO Auto-generated method stub
		String sql = "delete from "+BBS.TABLE_NAME +" where id="+id;
		return this.dao.delete(sql);
	}

}
